package com.jsmy.softapp.util;

import android.app.Activity;

import java.util.Stack;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/16.
 * Time: 11:13.
 * To change for file | settings | file and code templates.
 */

public class ActivityManager {
    private static volatile ActivityManager instance;

    private Stack<Activity> mActivityStack = new Stack<Activity>();

    private ActivityManager() {

    }

    public static ActivityManager getInstance() {
        if (instance == null) {
            synchronized (ActivityManager.class) {
                if (instance == null) {
                    instance = new ActivityManager();
                }
            }
        }
        return instance;
    }

    public void addActicity(Activity act) {
        mActivityStack.push(act);
    }

    public void removeActivity(Activity act) {
        if (act != null) {
            mActivityStack.remove(act);
            act.finish();
        }
    }

    public void removeAllActivity() {
        while (mActivityStack.size() > 0) {
            Activity act = mActivityStack.get(mActivityStack.size() - 1);
            removeActivity(act);
            act.finish();
        }
    }

    public void killMyProcess() {
        while (mActivityStack.size() > 0) {
            Activity act = mActivityStack.get(mActivityStack.size() - 1);
            removeActivity(act);
            act.finish();
        }

        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
